package com.example.demo.service;


import com.example.demo.dao.DrugsMapper;
import com.example.demo.entity.CommonResult;
import com.example.demo.entity.drugs;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.List;

@Slf4j
@Service
public class DrugService implements DrugServiceimpl {

    @Autowired
    DrugsMapper drugsMapper;

    @Override
    public List<drugs> Getalldrugs() {
        List<drugs> getalldrugs = drugsMapper.Getalldrugs();
        return getalldrugs;
    }

    @Override
    public CommonResult GetOnepagesdrug(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<drugs> getalldrugs = drugsMapper.Getalldrugs();
        if (getalldrugs.size() <= 0) {
            return new CommonResult(200, "仓库药品数量为0");
        }

        PageInfo pageInfo = new PageInfo(getalldrugs);
        return new CommonResult(200, "药品第" + pageInfo.getPageNum() + "页获取成功", pageInfo);
    }

    @Override
    public CommonResult Adddrugs(drugs drugs) {
        int adddrugs = drugsMapper.Adddrugs(drugs);
        if (adddrugs > 0) {
            return new CommonResult(200, "药品添加成功");
        }
        return new CommonResult(400, "药品添加失败，请重新添加");
    }

    @Override
    public CommonResult Updatedrugs(drugs drugs) {
        if (drugs.getDrug_id() != null && drugs.getId() != 0) {
            List<drugs> d01 = drugsMapper.selectdrugs(drugs.getId(), drugs.getDrug_id());
            if (d01.size() > 0 ) {
                int updatedrugs = drugsMapper.Updatedrugs(drugs);
                if(updatedrugs>0){
                    return new CommonResult(200, "药品修改成功");
                }
            }
            }
        return new CommonResult(400, "药品修改失败，没有该药品！！");
    }

    @Override
    public CommonResult deletedrugs(Integer id, String drug_id) {
        int deletedrugs = drugsMapper.deletedrugs(id, drug_id);
    if (deletedrugs>0){
        return new CommonResult(200, "药品删除成功");

    }
        return new CommonResult(400, "药品删除失败，没有该药品");
    }

    @Override
    public CommonResult selectdrugs(String drug_id,String drug_name) {
        List<drugs> selectdrug = drugsMapper.selectdrug(drug_id,drug_name);
        return new CommonResult(200,"商品信息",selectdrug);
    }


}
